<?php

	$this->AddHook('before_other_work', 'Wall2CreatePosts');
	$this->AddHook('after_other_work', 'Wall2ChkVersion');
	
	/*
	 * the actual post thingy
	 */
	function Wall2CreatePosts($bot)
	{
		global $CurrentUserId;
		
		if ($bot->firstrun)
		{ 
			//not work if it's first bot cycle
			$Name = "Wall2"; $Version = "1.0"; $Date = "2012-03-20";
			$bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date )  ;
			return;
		}
		
		$bot->ReloadConfig();
		$data = $bot->ld->GetPlSettings("Wall2");
			    
	    $bot->SendMsg('Wall2: Started for: '. $CurrentUserId);
	    
	    
	    include_once("class/wallDB.php");
	    
	    $db = wallDB::getInstance();
	    
	    $res = $db->query("SELECT * FROM myrequests WHERE nextWill<".time());
	    $arReqs = array();
	    while($dt = $res->fetchArray())
	    	array_push($arReqs, $dt);
	    	
	    $bot->SendMsg('Wall2: will try to post '. count($arReqs). " items");	
	    
	    include_once("Plugins/Wall2/class/wallRequests.php");
	    
	    $arAllDumps = array();
	    $sessionRequestsCount = 0;
	    foreach($arReqs as $arR)
	    {
	   		$req = $arR["id"];
	   		$arDump = wallRequests::makeRequest($req, $bot);
	   		
	   		$bot->SendMsg("tried $req and got: ".$arDump["result"]);
	   		
	   		//update my db
	   		$t = $db->getExpTimeForId($req);
	   		$nextTime = time() + $t * 60 * 60;

	   		$db->query("UPDATE myrequests SET lastWas=".time().", nextWill=$nextTime,reqCount=reqCount+1 WHERE id='$req' ");
	   		
	    	//post to sites?
	   	 	if ($arDump["result"]=="OK")
	   		{
	   			$sessionRequestsCount++;
	   			
	   			if ($arR["postToCVEx"])
	   			{
		   			$res1 = wallRequests::postToCVExchange($arDump);
		   			$bot->SendMsg("Cityville Exchange replied: $res1");
	   			}
	   			
	   			if ($arR["postToCVLinkEx"])
	   			{
		   			$res2 = wallRequests::postToCVLinkExchange($arDump);
		   			$bot->SendMsg("Cityville Link Exchange replied: $res2");
	   			}
	   			
	   			if ($arR["postToFile"])
	   				array_push($arAllDumps, $arDump);
	   				
	   			//chk if to remove
	   			if (
	   				(intval($arR["maxWanted"])>0) &&
		   			(intval($arR["reqCount"])+1 >= intval($arR["maxWanted"]))
		   			)
	   			{
	   				//remove this requests
	   				$db->query("DELETE FROM myrequests WHERE id='$req'");
	   			}
	   		}
	    }
	    
	    //dump file
	    if (!empty($arAllDumps))
	    {
		
			include_once "Plugins/Wall2/class/mustache/Mustache.php";
			include_once( "class/mustache/MustacheLoader.php" );
			
			$ml = new MustacheLoader("Plugins/Wall2/templates/","tpl");
			
	    	$m = new Mustache();
	    	$tplData = new stdClass();
	    	$tplData->arLinks = $arAllDumps;
	    	
	    	$dumpHtml = $m->render( file_get_contents("Plugins/Wall2/templates/htmlDump.tpl"), $tplData, $ml );
	    	unset($m);
	    	
	    	$fileName = "wall2Dump_".date("dmy_Hi").".html";
	    	file_put_contents("tmp_dir/Wall2/".$fileName, $dumpHtml);
	    }
	    
	    $tot = intval($db->getSetting('totalLinksCreated'));
	    $db->setSetting('totalLinksCreated', $tot+$sessionRequestsCount);
	    
	    //cleanup
	    include_once "Plugins/Wall2/class/wallMaintainance.php";
	    wallMaintainance::clearFiles();
	    
	    
	}
	
	/*
	 * Hook to run every time in order to keep DB up to date
	 */
	function Wall2ChkVersion($bot)
	{
		if ($bot->firstrun)
		{ 
			return;
		}
		
		global $currentUserId;
		
		
		include_once("Plugins/Wall2/class/wallDB.php");
		$db = wallDB::getInstance();;
		
		$curVers = $bot->GetCurrentRevision();
		$wall2Ver = $db->getParsedVersion();
		
		if ($wall2Ver==$curVers)
			return;
			
		$bot->SendMsg("Wall2: Upading Virals for version: ".$curVers);
		
		include_once("Plugins/Wall2/class/wallMaintainance.php");
		
		wallMaintainance::parseXML();
	}
?>